<?php 
namespace app\admin\controller;
use think\Db;
use app\common\controller\Adminbase;
use think\facade\Session;
use app\admin\model\Gtask as TaskModel;
use app\admin\model\Product as ProductModel;
use app\admin\model\Procedure as ProcedureModel;
use app\admin\model\AdminUser;
use app\admin\model\Gongdan as GongdanModel;
use app\admin\model\Wages as WagesModel;
class Wages extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $this->modelClass = new WagesModel();
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = $this->modelClass->where($where)->where('deleted',0)->order('id DESC')->page($page, $limit)->select();
            foreach($_list as $k=>$row){
                $product = ProductModel::find($row['product_id']);
                $_list[$k]['product_nums'] = $product['nums'];
                $_list[$k]['product_title'] = $product['title'];
                $procedure = ProcedureModel::find($row['procedure_id']);
                $_list[$k]['procedure_title'] = $procedure['title'];
                $_list[$k]['ftime'] = ftime($row['minutes']*60);
                
                $_list[$k]['type'] = $row['type'] == 1?'计件':'计时';
                $_list[$k]['admin'] = AdminUser::getAdmin($row['add_admin'],'nickname');
            }
            $count = $this->modelClass->where($where)->where('deleted',0)->count();
            $result = array("code" => 0, 'count' => $count, "data" => $_list);
            return $result;
        }
        return $this->fetch();
    }

    public function add()
    {
        if($this->request->post()){
            $data = $this->request->param();
            if(!$data['product_id']){
                $this->error('请选择产品');
            }
            if(!$data['procedure_id']){
                $this->error('请选择工序');
            }
            $minute = (60*(int)$data['hours'])+(int)$data['minutes'];
            $data['minutes'] = $minute;
            unset($data['hours']);
            unset($data['seconds']);
            unset($data['dialog']);
            $data['addtime'] = date('Y-m-d H:i:s');
            $data['add_admin'] = session('admin_user_auth')['uid'];
            $insert = WagesModel::insert($data);
            if($insert){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        //产品
        $product = ProductModel::where('deleted',0)->field('id,nums')->select();
        //工序
        $procedureList = ProcedureModel::where('deleted',0)->field('id,title')->select();
        $this->assign('product',$product);
        $this->assign('procedureList',$procedureList);
        return $this->fetch();
    }

    public function edit()
    {
        if($this->request->post()){
            $data = $this->request->param();
            $minute = (60*(int)$data['hours'])+(int)$data['minutes'];
            $data['minutes'] = $minute;
            $id = $data['id'];
            unset($data['hours']);
            unset($data['seconds']);
            unset($data['dialog']);
            unset($data['id']);
            $data['mdtime'] = date('Y-m-d H:i:s');
            $data['md_admin'] = session('admin_user_auth')['uid'];
            WagesModel::where(['id'=>$id])->update($data);
            $this->success('编辑成功');
        }
        $id = $this->request->param('id');
        //当前配置
        $wages = WagesModel::find($id);
        $product = ProductModel::field('id,title')->find($wages['product_id']);
        $wages['title'] = $product['title'];
        //产品
        $product = ProductModel::where('deleted',0)->field('id,nums')->select();
        //工序
        $procedureList = ProcedureModel::where('deleted',0)->field('id,title')->select();
        $wages['hours'] = (int)intval($wages['minutes']/60);
        $wages['minutes'] = $wages['minutes']-($wages['hours']*60);
        $this->assign('product',$product);
        $this->assign('wages',$wages);
        $this->assign('procedureList',$procedureList);
        return $this->fetch();
    }
    public function delete()
    {
        $data = $this->request->param();
        WagesModel::where('id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }

    function findProductName()
    {
        $id = $this->request->param('id');
        $product = ProductModel::field('id,title')->find($id);
        return json(['title'=>$product['title']]);
    }
}